/**
 * @author jaanush
 */

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit.Tooltip");
dojo.require("dojo.fx");
dojo.require("dojo.NodeList-fx");
dojo.require("dojox.fx.ext-dojo.NodeList");



dojo.provide("slipstream.store.Product");
dojo.provide("slipstream.store.ProductCart");
dojo.provide("slipstream.store.ProductCartFree");

dojo.declare("slipstream.store.Product", [dijit._Widget, dijit._Templated], {
	templatePath: dojo.moduleUrl("slipstream.store", "templates/Product.html"),
	data: {},
	postCreate: function(){
		this.catTopic=dojo.subscribe("category", this, 'changeCat');
		//var tt=dijit.showTooltip(this.data.text,this.domNode,'after');
		//var tt=new dijit.Tooltip({label:"programmatically created tooltip", connectId:[this.id]});
		//console.debug(tt);
	},
	postMixInProperties: function(){
		this.cat=this.data._S.getValue(this.data,'prod_struct_idprod_struct');
		this.price=this.data._S.getValue(this.data,'price');
		this.prodId=this.data._S.getValue(this.data,'idprod_detail');
		this.text=this.data._S.getValue(this.data,'text');
		this.name=this.data._S.getValue(this.data,'name');
		//this.label=this.name;
		this.img='/tempimages/'+this.prodId+'_0.jpg';
	},
	changeCat: function(arg){
		if (arg == this.cat) {
			//console.debug('show' + this.title);
			this.domNode.style.display= 'block';
		} else {
			//console.debug('hide' + this.title);
			this.domNode.style.display= 'none';
		};
	},
	_onMouseOver: function(e){
		//console.debug(e);
		dojo.fadeOut({
			duration: 200,
			node: e.currentTarget
		}).play();
		//console.debug(dojo.query('.fade',e.target));
		/*node: dojo.query('.fade',e.target).fadeOut({
			duration: 200
		}).play();*/
		//dojox.fx.crossFade({nodes: ["node3","node4"],duration:1000}).play();

	},
	_onMouseLeave: function(e){
		/*node: dojo.query('.fade',e.target).fadeIn({
			duration: 200
		}).play();*/
		dojo.fadeIn({
			duration: 200,
			node: e.currentTarget
		}).play();
		//console.debug(e);
	}
});

dojo.declare("slipstream.store.ProductCart", [slipstream.store.Product], {
	templateString: '<div prodId="${prodId}" class="slipstreamStoreProductCart"><img class="prodImage" src="${img}" alt="${title}" /><div class="overlay">${quantity}</div></div>',
	data: {},
	quantity: 1,
	postCreate: function(){
	},
	getQuantity: function(){
		return this.quantity;
	},
	setQuantity: function(numitems){
		this.quantity=numitems;
		//console.debug(this.domNode.lastChild.firstChild);
		this.domNode.lastChild.firstChild.nodeValue=this.quantity;
	}
});

dojo.declare("slipstream.store.ProductCartFree", [slipstream.store.ProductCart], {
	templateString: '<div prodId="${prodId}" class="slipstreamStoreProductCart frittval"><img class="prodImage" src="${img}" alt="Fritt val" /><div class="overlay">${quantity}x${price}</div></div>',
	data: {},
	title: 'Fritt val',
	quantity: 1,
	/*
	postCreate: function(){
		//this.prodId=this.id;
	},
	*/
	postMixInProperties: function(){
		console.debug(this.data,'CartProd data');
		this.cat=this.data.prod_struct_idprod_struct;
		this.price=this.data.price;
		this.prodId=this.data.idprod_detail;
		this.text=this.data.frittval;
		this.quantity=this.data.quantity||1;
		this.name=this.data.name || 'Fritt val';
		this.img='/gui/cart-thumb-frittval.gif';
	},
	/*
	getQuantity: function(){
		return this.quantity;
	},*/
	setQuantity: function(numitems){
		this.quantity=numitems;
		console.debug(this.domNode.lastChild.firstChild);
		this.domNode.lastChild.firstChild.nodeValue=this.quantity+'x'+this.price;
	}
});